package cn.blackbirds.blocks.layer;

import cn.blackbirds.blocks.Globals;
import cn.blackbirds.blocks.MainActivity;
import cn.blackbirds.blocks.R;

import com.wiyun.engine.actions.DelayTime;
import com.wiyun.engine.actions.FiniteTimeAction;
import com.wiyun.engine.actions.Hide;
import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.actions.Sequence;
import com.wiyun.engine.actions.Show;
import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.nodes.AtlasLabel;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.CharMap;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.transitions.ColorFadeTransition;
import com.wiyun.engine.transitions.TransitionScene;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYColor4B;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.types.WYSize;

public class GameOverLayer extends ColorLayer{
	
	private MainActivity mainActive;
	private GameLayer gameLayer;
	
	protected GameOverLayer(String score, MainActivity mainActive, GameLayer gameLayer) {		
		super(new WYColor4B(0, 0, 0, 180));		
		setTouchEnabled(true);					
		this.mainActive =  mainActive;
		this.gameLayer = gameLayer;
		//显示游戏结束
		showGameOver();        
        //显示最终得分
		showYourScore(score);
		
		autoRelease();

	}
	
	/**
	 * 显示Game Over
	 */
	private void showGameOver(){
		WYSize s = Director.getInstance().getWindowSize();
		Sprite gameOver = Sprite.make(R.drawable.gameover);
		gameOver.setVisible(false);
        addChild(gameOver);
        gameOver.setPosition(s.width / 2, s.height / 2);        
        IntervalAction seq = (IntervalAction)Sequence.make(        		
        		(FiniteTimeAction)DelayTime.make(1).autoRelease(),
        		(FiniteTimeAction)Show.make().autoRelease(),
        		(FiniteTimeAction)DelayTime.make(3).autoRelease(),
        		(FiniteTimeAction)Hide.make().autoRelease()).autoRelease();
        
        gameOver.runAction(seq);               
	}
	
	/**
	 * 显示Your Score
	 */
	private void showYourScore(String score){
		WYSize wySize = Director.getInstance().getWindowSize();
		// create charmap
    	CharMap map1 = CharMap.make();
    	map1.autoRelease();
    	float width = 28;
    	float height = 40;
		map1.mapChar(WYRect.make(0, 0, width, height), '0');
		map1.mapChar(WYRect.make(width, 0, width, height), '1');
		map1.mapChar(WYRect.make(width*2, 0, width, height), '2');
		map1.mapChar(WYRect.make(width*3, 0, width, height), '3');
		map1.mapChar(WYRect.make(width*4, 0, width, height), '4');
		map1.mapChar(WYRect.make(width*5, 0, width, height), '5');
		map1.mapChar(WYRect.make(width*6, 0, width, height), '6');
		map1.mapChar(WYRect.make(width*7, 0, width, height), '7');
		map1.mapChar(WYRect.make(width*8, 0, width, height), '8');
		map1.mapChar(WYRect.make(width*9, 0, width, height), '9');
		map1.mapChar(WYRect.make(width*10, 0, width, height), '+');
    	
		Sprite yourScoreStr = Sprite.make(R.drawable.yourscore);
		yourScoreStr.setPosition(wySize.width / 2-80, wySize.height / 2);  
        yourScoreStr.setVisible(false);
        addChild(yourScoreStr);        
        IntervalAction seq = (IntervalAction)Sequence.make(
        		(FiniteTimeAction)DelayTime.make(5).autoRelease(),
        		(FiniteTimeAction)Show.make().autoRelease(),
        		(FiniteTimeAction)DelayTime.make(3).autoRelease(),
        		(FiniteTimeAction)Hide.make().autoRelease()).autoRelease();       
        yourScoreStr.runAction(seq);
        
        Texture2D texture1 = Texture2D.makePNG(R.drawable.click_score);	    	
    	AtlasLabel yourScore = AtlasLabel.make("0", texture1, map1);       
    	yourScore.setPosition(wySize.width/2+80, wySize.height/2);
        yourScore.setText(score); 
        yourScore.setVisible(false);
        addChild(yourScore);        
        IntervalAction seq1 = (IntervalAction)Sequence.make(
        		(FiniteTimeAction)DelayTime.make(5).autoRelease(),
        		(FiniteTimeAction)Show.make().autoRelease(),
        		(FiniteTimeAction)DelayTime.make(3).autoRelease(),
        		(FiniteTimeAction)Hide.make().autoRelease()).autoRelease(); 
        yourScore.runAction(seq1);        
        
        seq1.setCallback(drawButton);             
		
	}
	
	 /**
	 * 绘制按钮和图片
	 */
	Callback drawButton = new Callback(){		
		
		@Override
		public void onStart(int actionPointer) {

		}

		@Override
		public void onStop(int actionPointer) {
			drawReplay();
		}

		@Override
		public void onUpdate(int arg0, float arg1) {
			
		}	
	};
	
	
	public void onRestarClicked() {
		mainActive.runOnUiThread(new Runnable() {
			public void run() {
				Globals.playButton();
				gameLayer.gameover();
				GameScene scenePlay = new GameScene(mainActive);				
				scenePlay.autoRelease();
	            Director.getInstance().replaceScene((TransitionScene)ColorFadeTransition.make(1, scenePlay, new WYColor3B(0, 0, 0)).autoRelease());
			}
		});					
    }			
	
	public void onMainmenuClicked() {
		mainActive.runOnUiThread(new Runnable() {
			public void run() {
				Globals.playButton();
				gameLayer.gameover();
				Globals.onPlayBg();
	            Director.getInstance().popSceneWithTransition((TransitionScene)ColorFadeTransition.make(1, null, new WYColor3B(0, 0, 0)).autoRelease());
			}
		});					
    }		
	
	private void drawReplay(){
		WYSize s = Director.getInstance().getWindowSize();
		
		Sprite sprite = Sprite.make(R.drawable.pause_background);
        sprite.setPosition(s.width/2, s.height*2);
        addChild(sprite); 
        IntervalAction seq = (IntervalAction)Sequence.make(
        		(MoveTo)MoveTo.make(1, sprite.getPositionX(), sprite.getPositionY(), s.width/2, s.height/2).autoRelease(),
        		(MoveTo)MoveTo.make((float)0.2, s.width/2, s.height/2, s.width/2, s.height/2+50).autoRelease(),
        		(MoveTo)MoveTo.make((float)0.1, s.width/2, s.height/2+50, s.width/2, s.height/2).autoRelease()
        ).autoRelease();
              
        
			
        Button restart = Button.make(R.drawable.pause_restart, R.drawable.pause_restart_pressed, this, "onRestarClicked");
        restart.setPosition(s.width/2, s.height*2 + (35+12));   
        addChild(restart);                   
        IntervalAction seq1 = (IntervalAction)Sequence.make(
        		(MoveTo)MoveTo.make(1, sprite.getPositionX(), sprite.getPositionY(), s.width/2, s.height/2+ (35+12)).autoRelease(),
        		(MoveTo)MoveTo.make((float)0.2, s.width/2, s.height/2+ (35+12), s.width/2, s.height/2+50+ (35+12)).autoRelease(),
        		(MoveTo)MoveTo.make((float)0.1, s.width/2, s.height/2+50+ (35+12), s.width/2, s.height/2+ (35+12)).autoRelease()
        ).autoRelease();
        
        
        Button mainmenu = Button.make(R.drawable.gamveover_mainmenu, R.drawable.gamveover_mainmenu_p, this, "onMainmenuClicked");
        mainmenu.setPosition(s.width/2, s.height*2 - (35+12));  
        addChild(mainmenu);              
        IntervalAction seq2 = (IntervalAction)Sequence.make(
        		(MoveTo)MoveTo.make(1, sprite.getPositionX(), sprite.getPositionY(), s.width/2, s.height/2- (35+12)).autoRelease(),
        		(MoveTo)MoveTo.make((float)0.2, s.width/2, s.height/2- (35+12), s.width/2, s.height/2+50- (35+12)).autoRelease(),
        		(MoveTo)MoveTo.make((float)0.1, s.width/2, s.height/2+50- (35+12), s.width/2, s.height/2- (35+12)).autoRelease()
        ).autoRelease();
        
        sprite.runAction(seq);
        restart.runAction(seq1);
        mainmenu.runAction(seq2);
	}
	
}
